package com.ruicar.afs.cloud.product.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruicar.afs.cloud.product.condition.ProductUpdatePlanCondition;
import com.ruicar.afs.cloud.product.entity.ProductUpdatePlan;
import com.ruicar.afs.cloud.product.enums.ATOM_CONTROL_TYPE;
import com.ruicar.afs.cloud.product.mapper.ProductUpdatePlanMapper;
import com.ruicar.afs.cloud.product.service.ProductUpdatePlanService;
import com.ruicar.afs.cloud.product.vo.*;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author xudongdong
 * @version 1.0
 * @date 2020-05-22
 */
@Service
public class ProductUpdatePlanServiceImpl extends ServiceImpl<ProductUpdatePlanMapper, ProductUpdatePlan> implements ProductUpdatePlanService {

    @Override
    public IPage<List<ProductPrototypeVo>> getProductPlanList(Page page, ProductUpdatePlanCondition productCondition) {
        return this.baseMapper.getProductPlanList(page, productCondition);
    }

    @Override
    public IPage<List<ProductPrototypeVo>> getPlanData(Page page, ProductUpdatePlanCondition productCondition) {
        return this.baseMapper.getPlanData(page, productCondition);
    }

    @Override
    public List<ProductPlanVo> getVueInfo(ProductUpdatePlanCondition productCondition) {
        return this.baseMapper.getVueJs(productCondition);
    }

    @Override
    public List<ProductPlanVo> getOldPlanList(Long id) {
        return this.baseMapper.getOldPlanList(id);
    }

    @Override
    public List<ProductPlanVo> getOldPlanData(Long id) {
        return this.baseMapper.getOldPlanData(id);
    }

    @Override
    public List<ProductPlanVo> getVueApproveInfo(Long id, Long productTemplateId) {
        return this.baseMapper.getVueApproveInfo(id, productTemplateId);
    }

    @Override
    public Map<String, Object> getPlanApproveInfo(Long id) {
        List<ProductPlanVo> list = this.baseMapper.getOldPlanList(id);
        Map<String, Object> map = new HashMap<>();
        list.stream().forEach(
                productPlanVo -> {
                    Object value = "";
                    if (StrUtil.isNotBlank(productPlanVo.getValue())) {
                        String type = ATOM_CONTROL_TYPE.getType(productPlanVo.getType());
                        if (ATOM_CONTROL_TYPE.DECIMAL.getType().equals(type)) {
                            value = new BigDecimal(productPlanVo.getValue());
                        } else if (ATOM_CONTROL_TYPE.AMOUNT.getType().equals(type)) {
                            value = new BigDecimal(productPlanVo.getValue());
                        } else if (ATOM_CONTROL_TYPE.DATE.getType().equals(type)) {
                            value = DateUtil.parse(productPlanVo.getValue(), "yyyy-MM-dd");
                        } else if (ATOM_CONTROL_TYPE.INTEGER.getType().equals(type)) {
                            value = Integer.valueOf(productPlanVo.getValue());
                        } else if (ATOM_CONTROL_TYPE.TIME.getType().equals(type)) {
                            value = DateUtil.parse(productPlanVo.getValue(), "HH:mm:ss");
                        } else {
                            value = productPlanVo.getValue();
                        }
                    }
                    map.put(productPlanVo.getKey(), value);
                }
        );
        return map;
    }

    //根据列表得到最终的展示结果
    public VueVo getVueVo(List<ProductPlanVo> list) {
        List<VueJs> vueJsList = new ArrayList<>();
        list.stream().forEach(
                productPlanVo -> {
                    VueJs vueJs = new VueJs(productPlanVo);
                    if (ATOM_CONTROL_TYPE.SCALE.getType().equals(vueJs.getType()) || ATOM_CONTROL_TYPE.INTEGER.getType().equals(vueJs.getType())) {
                        vueJs.setType(ATOM_CONTROL_TYPE.INTEGER.getType());
                        vueJs.setScale(2);
                    }
                    if (ATOM_CONTROL_TYPE.RADIO.getType().equals(vueJs.getType())) {
                        vueJs.setType(ATOM_CONTROL_TYPE.SINGLE_SELECT.getType());
                    }
                    vueJs.setRequire(false);
                    vueJsList.add(vueJs);
                }
        );
        Map<String, List<VueJs>> map = new HashMap<>();
        map.put("approve", vueJsList);
        VueVo vueVo = new VueVo(map);
        vueVo.getFormOptionsVoList().get(0).setIsGroup(false);
        vueVo.setTotal(vueVo.getVueJsList().size());
        return vueVo;
    }

    @Override
    public IPage<List<ProductUpdatePlanVo>> queryProductUpdatePlanList(Page page, Long id){

        return this.baseMapper.queryProductUpdatePlanList(page,id);
    }


    @Override
    public List<ProductPlanVo> getNewPlanList(Long id) {
        return this.baseMapper.getNewPlanList(id);
    }

    @Override
    public List<ProductPlanVo> getNewPlanData(Long id) {
        return this.baseMapper.getNewPlanData(id);
    }
}
